package online.foxnull2.foxmall.product.controller;

import online.foxnull2.common.utils.R;
import online.foxnull2.foxmall.product.entity.AttrEntity;
import online.foxnull2.foxmall.product.entity.AttrGroupEntity;
import online.foxnull2.foxmall.product.service.AttrAttrgroupRelationService;
import online.foxnull2.foxmall.product.service.AttrGroupService;
import online.foxnull2.foxmall.product.service.AttrService;
import online.foxnull2.foxmall.product.service.CategoryService;
import online.foxnull2.foxmall.product.utils.PageUtils;
import online.foxnull2.foxmall.product.vo.AttrGroupRelationVo;
import online.foxnull2.foxmall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author foxnull2
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    private final AttrGroupService attrGroupService;
    private final CategoryService categoryService;
    private final AttrService attrService;
    private final AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    public AttrGroupController(AttrGroupService attrGroupService,
                               CategoryService categoryService,
                               AttrService attrService,
                               AttrAttrgroupRelationService attrAttrgroupRelationService) {
        this.attrGroupService = attrGroupService;
        this.categoryService = categoryService;
        this.attrService = attrService;
        this.attrAttrgroupRelationService = attrAttrgroupRelationService;
    }

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> relationAttrs = attrService.getRelationAttrs(attrGroupId);
        return R.ok().put("relationAttrs", relationAttrs);
    }

    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrService.getRelationNoattrs(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表{catelogId}
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    @GetMapping("/{catelogId}/withattr")
    public R listAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVoList = attrGroupService.listAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("attrGroupWithAttrsVoList", attrGroupWithAttrsVoList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    @RequestMapping("/info/catelogIdPath/{attrGroupId}")
    public R infoCatelogIdPath(@PathVariable("attrGroupId") Long attrGroupId) {
        List<Long> catelogIdPath = categoryService.getCatalogIdPath(attrGroupId);

        return R.ok().put("catelogIdPath", catelogIdPath);
    }

    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVoList) {
        attrAttrgroupRelationService.saveBatch(attrGroupRelationVoList);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R delete(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVoList) {
        attrGroupService.deleteRelations(attrGroupRelationVoList);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
